<?php
/**
 * Created by PhpStorm.
 * User: yangc
 * Date: 2017/12/29
 * Time: 16:07
 */

namespace Xiaoc\Api;

/**
 * Xiaoc 秒杀活动
 * Class Seckill
 * @package Xiaoc\Api
 */
class Seckill extends Common
{
    public function getRules()
    {
        return array(
            'goodsList' => array(
                'page' => array('name' => 'page', 'default' => 1, 'desc' => '页数'),
                'frist_time_id' => array('name' => 'first_time_id', 'require' => true, 'desc' => '首次秒杀id')
            ),

        );
    }

    /**
     * 秒杀时间列表
     * @return array 秒杀时间列表
     */
    public function timeList()
    {
        $time_list = get_seckill_time();
        $frist_time = array_slice($time_list, 0, 1);
        foreach ($frist_time as $v) {
            $frist_time_id = $v['id'];
        }
        foreach ($time_list as $key => $value) {
            if ($value['id'] == $frist_time_id) {
                $time_list[$key]['active'] = 1;
            }
        }
        return $time_list;
    }

    /**
     * 秒杀列表
     */
    public function goodsList()
    {
        $prefix = \PhalApi\DI()->config->get('dbs.tables.__default__.prefix');
        $tmr = 86400 * 7;
        $id = $this->frist_time_id;
        $tomorrow = 0;
        $sql = 'select begin_time,end_time FROM ' . $prefix . 'seckill_time_bucket where id=' . $id;
        $times = \PhalApi\DI()->notorm->seckill_time_bucket->queryAll($sql, array());
        $begin_time = date('Y-m-d H:i:s', strtotime($times[0]['begin_time']));
        $end_time = date('Y-m-d H:i:s', strtotime($times[0]['end_time']));
        $local_begin_time = strtotime($times[0]['begin_time']);
        $local_end_time = strtotime($times[0]['end_time']);
        if ($tomorrow == 1) {
            $begin_time = date('Y-m-d H:i:s', strtotime($times[0]['begin_time']) + $tmr);
            $end_time = date('Y-m-d H:i:s', strtotime($times[0]['end_time']) + $tmr);
            $local_begin_time = strtotime($times[0]['begin_time']) + $tmr;
            $local_end_time = strtotime($times[0]['end_time']) + $tmr;
        }

        $seckill_status = seckill_status($local_begin_time, $local_end_time);
        if (!$seckill_status['is_end'] && !$seckill_status['status']) {
            $end_time = $begin_time;
        }
        $this->size = 5;
        $page = $this->page;
        $list = seckill_goods_results($id, $page, $this->size, $tomorrow);
        exit(json_encode(array('gb_list' => $list, 'totalPage' => $list['totalPage'], 'page' => $page, 'end_time' => $end_time, 'seckill_status' => $seckill_status)));
    }
}